import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRoute, UrlSegment } from '@angular/router';

import { UtilService } from '../my-service/util.service';

@Injectable()
export class LoginGuard implements CanActivate {
  path: string = '';
  constructor(
    private utilService: UtilService,
    private router: Router,
    private route: ActivatedRoute
  ) { }

  canActivate() {
    this.route.url.subscribe((s: UrlSegment[]) => {
      this.path = s[0].path;
    });

    let result = this.utilService.getUserInfo()
      .then(res => {
        if (res.status == 1) return true;
        else {
          if (this.path == "" || this.path == "home") {
            this.router.navigateByUrl('/contentStore/content')
          }
          else this.utilService.remind('error', '请先登录')
          return false;
        }
      })
    return result;
  }
}